package it.tukano.swingdeco.table;

import it.tukano.fun.Function2;
import it.tukano.swingdeco.controls.JComboBoxExt;
import java.awt.Component;
import java.util.Collection;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

public class ComboBoxTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    
    //Default serial version code
    private static final long serialVersionUID = 0;
    
    private final JComboBoxExt box = new JComboBoxExt();
    private Object inputValue;
    private Function2<Integer, Integer, Collection<? extends Object>> dataBinding;

    /**
     * Instance initializer
     */
    public ComboBoxTableCellEditor() {
    }
    
    public void setChoosableValues(Collection<? extends Object> values) {
        box.setValues(values);
    }
    
    public void setInputDataBinding(Function2<Integer, Integer, Collection<? extends Object>> binding) {
        this.dataBinding = binding;
    }

    public Object getCellEditorValue() {
        return box.getSelectedItem();
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        inputValue = value;
        if(dataBinding != null) box.setValues(dataBinding.apply(row, column));
        box.setSelectedItem(value);
        return box;
    }
}
